//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include "general.h"

#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"

CSQLiteQueryImpl::CSQLiteQueryImpl(CSQLiteDBImpl *pDB)
    :  mDB(pDB)
    ,  mStatement(nullptr)
    ,  mSkipNextRow(false)
{
    mStatement=nullptr;
}

CSQLiteQueryImpl::~CSQLiteQueryImpl()
{
    if(mStatement)
        sqlite3_finalize(mStatement);
}

void CSQLiteQueryImpl::Prepare(const std::string &pQuery)
{
    const char *lUnused;

    int lRC=sqlite3_prepare_v2(mDB->GetConnection(),pQuery.c_str(),pQuery.length(),
                    &mStatement,&lUnused);

    if(lRC!=SQLITE_OK)
    {
        throw XSQLQueryFailed("can't prepare statement",lRC);
    }
}

void CSQLiteQueryImpl::BindBlob(int pN,const void *pValue,std::size_t pLen,bool pCopy)
{
    sqlite3_bind_blob(mStatement,pN,pValue,pLen,pCopy?SQLITE_TRANSIENT:SQLITE_STATIC);
}

void CSQLiteQueryImpl::BindInteger(int pN,int64_t pValue)
{
    sqlite3_bind_int64(mStatement,pN,pValue);
}

void CSQLiteQueryImpl::BindString(int pN,const std::string &pValue,bool pCopy)
{
    sqlite3_bind_text(mStatement,pN,pValue.c_str(),pValue.length(),
                                            pCopy?SQLITE_TRANSIENT:SQLITE_STATIC);
}

void CSQLiteQueryImpl::BindDouble(int pN,double pValue)
{
    sqlite3_bind_double(mStatement,pN,pValue);
}

void CSQLiteQueryImpl::BindNull(int pN)
{
    sqlite3_bind_null(mStatement,pN);
}

void CSQLiteQueryImpl::ResetBindings()
{
    sqlite3_clear_bindings(mStatement);
}

std::size_t CSQLiteQueryImpl::ColumnLength(int pN)
{
    return sqlite3_column_bytes(mStatement,pN);
}

const void *CSQLiteQueryImpl::ColumnBlob(int pN)
{
    return sqlite3_column_blob(mStatement,pN);
}

int64_t CSQLiteQueryImpl::ColumnInteger(int pN)
{
    return sqlite3_column_int64(mStatement,pN);
}

std::string CSQLiteQueryImpl::ColumnString(int pN)
{
    return std::string((const char*)sqlite3_column_text(mStatement,pN));
}

double CSQLiteQueryImpl::ColumnDouble(int pN)
{
    return sqlite3_column_double(mStatement,pN);
}

bool CSQLiteQueryImpl::ColumnNull(int pN)
{
    return (sqlite3_column_type(mStatement,pN)==SQLITE_NULL);
}

void CSQLiteQueryImpl::Exec()
{
    mSkipNextRow=false;
    mSkippedResult=NextRow();
    mSkipNextRow=true;
}

bool CSQLiteQueryImpl::NextRow()
{
    if(mSkipNextRow)
    {
        mSkipNextRow=false;
        return mSkippedResult;
    }

    int lRC=sqlite3_step(mStatement);

    if(lRC==SQLITE_ROW)
    {
        return true;
    }
    else if(lRC==SQLITE_DONE)
    {
        return false;
    }
    else
    {
        throw XSQLQueryFailed("my fault",lRC);
    }
}

void CSQLiteQueryImpl::Reset()
{
    sqlite3_reset(mStatement);
}

int64_t CSQLiteQueryImpl::GetLastInsertID()
{
    return mDB->GetLastInsertID();
}
